﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mimoza.Common;
using Mimoza.DBLayer.Transport;
using Mimoza.Transport;

namespace Mimoza.DBLayer.Transport.Client
{
    public class DBContainers :
        ClientRepository<ContainerRecord>,
        IDBContainerRepository
    {
        public DBContainers()
        {
            DBContainersTranferRequest.RegisterInTransport();
            RepositoryReply<ContainerRecord>.RegisterInTransport();
        }

        public ContainerRecord GetContainer(Guid ID)
        {
            return MakeObjectRequest(
                    DBContainersCustomRequest.CustomOperationType.GetContainer, ID);
        }

        public IEnumerable<ContainerRecord> GetChildsContainer(Guid containerID)
        {
            return MakeCollectionRequest<ContainerRecord>(
                    DBContainersCustomRequest.CustomOperationType.GetChildsContainer, 
                    containerID);
        }

        public IEnumerable<HostRecord> GetHosts(Guid containerID)
        {
            return MakeCollectionRequest<HostRecord>(
                    DBContainersCustomRequest.CustomOperationType.GetHosts, 
                    containerID);
        }

        public IEnumerable<PolicyRecord> GetPolicies(Guid containerID)
        {
            return MakeCollectionRequest<PolicyRecord>(
                    DBContainersCustomRequest.CustomOperationType.GetPolicies,
                    containerID);
        }

        override public void Insert(ContainerRecord container)
        {
            container = MakeObjectRequest(new DBContainersTranferRequest(Operation.Insert, container));
            base.Insert(container);
        }

        override public void Update(ContainerRecord container)
        {
            container = MakeObjectRequest(new DBContainersTranferRequest(Operation.Update, container));
            base.Update(container);
        }

        override public void Delete(ContainerRecord container)
        {
            container = MakeObjectRequest(new DBContainersTranferRequest(Operation.Delete, container));
            base.Delete(container);
        }

        ContainerRecord MakeObjectRequest(DBContainersCustomRequest.CustomOperationType oper, 
            Guid argument)
        {
            DBContainersCustomRequest customReq = new DBContainersCustomRequest(oper, argument.ToString());
            return MakeObjectRequest(new DBContainersTranferRequest(customReq));
        }

        ClientCollectionEnumerable<TCollectionRecord> MakeCollectionRequest<TCollectionRecord>(DBContainersCustomRequest.CustomOperationType oper,
            Guid argument)
            where TCollectionRecord : class, new()
        {
            DBContainersCustomRequest customReq = new DBContainersCustomRequest(oper, argument.ToString());
            return MakeCollectionRequest<TCollectionRecord>(new DBContainersTranferRequest(customReq));
        }
    }
}
